<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tree_menu/tree_menu_item_dbc.class.php');
require_once('tree_menu_ml_label_dbc.class.php');
require_once('te_tree_menu_ml_label_already_exists.class.php');


class Tree_Menu_Item_ML_DBC extends Tree_Menu_Item_DBC {
	private $lang;
	private $ml_labels = array();


	function __construct($table = 'menu', $lang) {
		$this->set_table($table);
		$this->set_lang($lang);
	}


	public function set_lang($lang) {
		$this->lang = $lang;
	}


	public function get_lang() {
		return $this->lang;
	}


	public function save(DB_Connection $dbc) {
		$ret = parent::save($dbc);

		$labels = $this->get_ml_labels();
		foreach($labels as $label) {
			$label->set_item($ret);
			$label->save($dbc);
		}
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = parent::_load_by_id($dbc, $id);

		$table = addslashes($this->get_table());

		$sql = "select id from $table where parent = $id order by `order` asc";
		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$item = new Tree_Menu_Item_ML_DBC($table, $this->get_lang());
			$item->load_by_id($dbc, $rez_obj->ID);
			$this->add_subitem(clone $item);
		}

		$this->load_ml_label($dbc, $id, $this->get_lang());

		return $ret;
	}


	public function load_by_id_full(DB_Connection $dbc, $id) {
		$ret = parent::_load_by_id($dbc, $id);

		$table = addslashes($this->get_table());

		$sql = "select id from $table where parent = $id order by `order` asc";

		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$item = new Tree_Menu_Item_ML_DBC($table, $this->get_lang());
			$item->load_by_id_full($dbc, $rez_obj->ID);
			$this->add_subitem(clone $item);
		}

		$this->load_ml_labels($dbc, $id);

		return $ret;
	}


	public function get_label() {
		$ml_label = $this->get_ml_label($this->get_lang());

		if ($ml_label && $ml_label instanceof Tree_Menu_ML_Label) {
			$ret = $ml_label->get_label();
		} else {

			$ret = parent::get_label();
		}

		return $ret;
	}


	public function set_ml_labels($ml_labels) {
		if (is_array($ml_labels)) {
			$this->ml_labels = $ml_labels;
		} else {
			throw new TE_Tree_Menu('$ml_labels is not an array.');
		}
	}


	public function get_ml_labels() {
		return $this->ml_labels;
	}


	public function add_ml_label(Tree_Menu_ML_Label $label) {
		if (!$this->ml_label_exists($label)) {
			$this->ml_labels[$label->get_language()] = $label;
		} else {
			throw new TE_Tree_Menu_ML_Label_Alreay_Exists($label->get_language());
		}
	}


	public function update_ml_label(Tree_Menu_ML_Label $label) {
		if ($this->ml_label_exists($label)) {
			$id = $this->ml_labels[$label->get_language()]->get_id();
			$label->set_id($id);
			$this->ml_labels[$label->get_language()] = $label;
		} else {
			throw new TE_Tree_Menu_ML_Label_Not_Exists($label->get_language());
		}
	}


	public function ml_label_exists(Tree_Menu_ML_Label $label) {
		return array_key_exists($label->get_language(), $this->ml_labels);
	}



	public function get_ml_label($lang) {
		if (array_key_exists($lang, $this->ml_labels)) {
			$ret = $this->ml_labels[$lang];
		} else {
			$ret = false;
		}

		return $ret;
	}


	protected function load_ml_label(DB_Connection $dbc, $item_id, $lang) {
		$ml_label = new Tree_Menu_ML_Label_DBC($this->get_table());
		if ($ml_label->load_by_item_and_language($dbc, $item_id, $lang)) {
			$this->add_ml_label($ml_label);
			$ret = $ml_label->get_id();
		} else {
			$ret = false;
		}

		return $ret;
	}


	public function load_ml_labels(DB_Connection $dbc, $item_id) {
		$labels = Tree_Menu_ML_Label_DBC::load_ml_labels_for_item($dbc, $item_id, $this->get_table());

		foreach($labels as $label) {
			$this->add_ml_label($label);
		}
	}


	public function update_subitem(Tree_Menu_Item_DBC $item) {
		if ($item instanceof Tree_Menu_Item_ML_DBC) {
			if ($this->subitem_exists($item)) {
				$orig_item = $this->subitems[$item->get_hid()];
				$orig_mls = $orig_item->get_ml_labels();

				$new_mls = $item->get_ml_labels();

				foreach($orig_mls as $orig_ml) {
					foreach($new_mls as $new_ml) {
						if ($new_ml->get_language() == $orig_ml->get_language()) {
							$new_ml->set_id($orig_ml->get_id());
							$item->update_ml_label($new_ml);
							break;
						}
					}
				}

				$item->set_id($orig_item->get_id());
				$item->set_subitems($orig_item->get_subitems());
				$this->subitems[$item->get_hid()] = $item;
			} else {
				throw new TE_Tree_Menu('Such subitem does not exists: '.$item->get_label());
			}
		} else {
			throw new TE_Tree_Menu('$item is not instance of Tree_Menu_Item_ML_DBC.');
		}
	}


	public static function create_from_import_data($raw_arr, $table = 'menu',$update_existing = false, $lang = 'en') {
		$ret = false;

		if (count($raw_arr) == 7) {
			$tmp = new Tree_Menu_Item_ML_DBC($table, $lang);
			$tmp->set_label($raw_arr[0]);
			$tmp->set_link($raw_arr[2]);
			$tmp->set_hid($raw_arr[3]);
			$tmp->set_target($raw_arr[4]);
			$tmp->set_order($raw_arr[5]);

			$ret['parent_hid'] = $raw_arr[1];

			foreach($raw_arr['ml_labels'] as $label) {
				$label_obj = new Tree_Menu_ML_Label_DBC($table);
				$label_obj->set_label($label['value']);
				$label_obj->set_language($label['lang']);

				if ($tmp->ml_label_exists($label_obj)) {

					if ($update_existing) {
						$tmp->update_ml_label($label_obj);
					}
				} else {
					$tmp->add_ml_label($label_obj);
				}
			}


			$ret['item'] = clone $tmp;
		}

		return $ret;
	}


	public function add_subitem(Tree_Menu_Item $item) {
		if ($item instanceof Tree_Menu_Item_ML_DBC) {
			parent::add_subitem($item);
		} else {
			throw new TE_Tree_Menu('$item is not instance of Tree_Menu_Item_ML_DBC');
		}
	}


	public static function delete_cascade(DB_Connection $dbc, $id, $db_tables_prefix = '') {

		parent::delete($dbc, $id, $db_tables_prefix.'menu');

		Tree_Menu_ML_Label_DBC::delete_all_labels_for_item($dbc, $id, $db_tables_prefix.'menu_ml_labels');
	}
}